 /*
 *  Gurr - A multiprotocol chat client for Android
 *  Copyright (C) 2011 Malte Aschermann, Wilfried Ramaker
 *  
 *  This file is part of Gurr.
 *  
 *  Gurr is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Gurr is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Gurr.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.masc.gurr.net.otr;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.masc.gurr.net.ICQ;


import ca.uwaterloo.crysp.otr.UserState;
import ca.uwaterloo.crysp.otr.crypt.jca.JCAProvider;
import ca.uwaterloo.crysp.otr.iface.OTRInterface;

public class OTRDriver {
	private static OTRDriver instance;

	/** protocol strings */
	public static final String PROTOCOL_ID_ICQ="prpl-oscar";
	
	/** otr control messages */
	public static final String OTR_INIT_MSG="?OTR:init"; 
	
	/** map recipient's uin to session */ 
	private Map<String, Session> fMapUinSession;
	
	/** encapsulates the list of known fingerprints and the list of private keys */
	private OTRInterface fUsICQ;
	
	public static OTRDriver getInstance() {
		if (instance == null)
			instance = new OTRDriver();
		return instance;
	}

	private OTRDriver() {
		fMapUinSession = new HashMap<String, Session>();
		fUsICQ = new UserState(new JCAProvider());
	}
		
	public Session getOTRSession(String pStrMyUin, String pStrProtocol, String pStrRecipientUin) throws IOException {
		
		if (! hasSession(pStrRecipientUin)) {

			Session s = new Session(pStrMyUin, pStrProtocol, pStrRecipientUin, fUsICQ);
			fMapUinSession.put(pStrRecipientUin, s);

			return s;
			
		} else {
			return fMapUinSession.get(pStrRecipientUin);
		}
	}
	
	public boolean hasSession(String pStrRecipientUin) {
		return fMapUinSession.containsKey(pStrRecipientUin);
	}

	protected OTRInterface getfUsICQ() {
		return fUsICQ;
	}
	
	public OTRState getOTRState(String pStrRecipientUin) {
		if (hasSession(pStrRecipientUin)) {
			return fMapUinSession.get(pStrRecipientUin).getOtrState();
		}
		return OTRState.UNDEF;
	}
}



